﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApMovMovieManagerNodeData
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Drawing;
using System.Resources;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands;

public class CApMovMovieManagerNodeData
{
  private int m_nodeLevel;
  private Bitmap m_nodeIcon;
  private ITxPlanningObject m_nodeObject;
  private int m_rowIndex;
  private bool m_isOperationSelected;
  private OrderedDictionary m_fileAttachmentsPathAndDate;
  private bool m_canEditNode;

  public CApMovMovieManagerNodeData(int nodeLevel, Bitmap nodeIcon, ITxPlanningObject nodeObject)
  {
    this.m_nodeLevel = nodeLevel;
    this.m_nodeObject = nodeObject;
    this.m_rowIndex = 0;
    this.m_nodeIcon = nodeIcon;
    this.m_isOperationSelected = false;
    this.m_fileAttachmentsPathAndDate = new OrderedDictionary();
    this.InitializeAllAttachments();
    this.RestoreMarkedActivityesStates();
    this.m_canEditNode = this.GetCheckInCheckOutStates(nodeObject);
  }

  public int NodeLevel => this.m_nodeLevel;

  public Bitmap NodeIcon => this.m_nodeIcon;

  public ITxPlanningObject NodeObject => this.m_nodeObject;

  public int RowIndex
  {
    get => this.m_rowIndex;
    set => this.m_rowIndex = value;
  }

  public bool IsNodeOperationSelected
  {
    get => this.m_isOperationSelected;
    set => this.m_isOperationSelected = value;
  }

  public bool IsNodeHasAVIAttachments => this.m_fileAttachmentsPathAndDate.Count > 0;

  public OrderedDictionary AttachmentsPathAndDate => this.m_fileAttachmentsPathAndDate;

  public bool CanAttachNewObject => this.m_canEditNode;

  public void RemoveAttachment(TxFileAttachment attachmentFile)
  {
    this.m_nodeObject.RemoveFileAttachment(attachmentFile);
    this.InitializeAllAttachmentsRemove(attachmentFile);
  }

  public void RemoveAllAttachments()
  {
    if (!this.IsNodeHasAVIAttachments)
      return;
    foreach (DictionaryEntry dictionaryEntry in this.m_fileAttachmentsPathAndDate)
      this.m_nodeObject.RemoveFileAttachment(dictionaryEntry.Key as TxFileAttachment);
  }

  public void AddAttachment(string newAttachmentFile)
  {
    this.InitializeAllAttachmentsAdd(this.m_nodeObject.AddFileAttachment(newAttachmentFile));
  }

  public string DownloadAttachment(string attachmentFile)
  {
    string str = "";
    if (this.IsNodeHasAVIAttachments && this.m_nodeObject.DoesFileAttachmentExist(attachmentFile))
    {
      foreach (DictionaryEntry dictionaryEntry in this.m_fileAttachmentsPathAndDate)
      {
        TxFileAttachment txFileAttachment = this.m_fileAttachmentsPathAndDate[dictionaryEntry.Key] as TxFileAttachment;
        if (txFileAttachment.FullPathInFileSystem == attachmentFile)
        {
          str = this.m_nodeObject.DownloadFileAttachment(txFileAttachment);
          break;
        }
      }
    }
    return str;
  }

  public bool CanRemoveAttachment(TxFileAttachment attachmentFile)
  {
    return this.m_nodeObject.CanRemoveFileAttachment(attachmentFile);
  }

  public void InitializeAllAttachments()
  {
    this.m_fileAttachmentsPathAndDate.Clear();
    TxFileAttachment[] fileAttachments = this.m_nodeObject.FileAttachments;
    int index = 0;
    for (int length = fileAttachments.Length; index < length; ++index)
    {
      if (fileAttachments[index].FileAttachmentType == 1)
        this.m_fileAttachmentsPathAndDate.Add((object) fileAttachments[index], (object) fileAttachments[index].FileAttachmentType);
    }
  }

  private void InitializeAllAttachmentsAdd(TxFileAttachment newAttachment)
  {
    if (this.m_fileAttachmentsPathAndDate.Contains((object) newAttachment))
      return;
    this.m_fileAttachmentsPathAndDate.Add((object) newAttachment, (object) newAttachment.FileAttachmentType);
  }

  private void InitializeAllAttachmentsRemove(TxFileAttachment oldAttachment)
  {
    this.m_fileAttachmentsPathAndDate.Remove((object) oldAttachment);
  }

  public void RestoreMarkedActivityesStates()
  {
    if (!(this.m_nodeObject.EngineeringRepresentation.GetAttribute(new ResourceManager("DnProcessSimulateCommands.StringTable", this.GetType().Assembly).GetString("MOVIE_MANAGER_ATTRIBUTE_ACTIVITY_SELECTED")) is TxIntAttribute attribute))
      return;
    this.m_isOperationSelected = Convert.ToBoolean(attribute.Value);
  }

  public void StoreMarkedActivityesStates()
  {
    TxIntAttribute txIntAttribute = new TxIntAttribute(new ResourceManager("DnProcessSimulateCommands.StringTable", this.GetType().Assembly).GetString("MOVIE_MANAGER_ATTRIBUTE_ACTIVITY_SELECTED"), Convert.ToInt32(this.m_isOperationSelected));
    if (txIntAttribute == null)
      return;
    this.m_nodeObject.EngineeringRepresentation.SetAttribute((TxAttribute) txIntAttribute);
  }

  public bool GetCheckInCheckOutStates(ITxPlanningObject obj)
  {
    bool inCheckOutStates = false;
    try
    {
      if (TxApplication.PlatformType == 1)
      {
        TxEmsServicesProvider servicesProvider = obj.PlatformServicesProvider as TxEmsServicesProvider;
        inCheckOutStates = servicesProvider.InternalId != 0 && servicesProvider.CheckInCheckOutState == 1 && ((ITxObject) obj).CanBeModified;
      }
      else if (TxTcUtilities.IsTcAnyPlatform)
        inCheckOutStates = ((ITxObject) obj).CanBeModified;
    }
    catch
    {
      inCheckOutStates = false;
    }
    return inCheckOutStates;
  }
}
